package com.wenx.activeMqCamel;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

import javax.jms.ConnectionFactory;

import static org.apache.camel.language.constant.ConstantLanguage.constant;

public class WebServiceProxy {
    private static String user = "admin";
    private static String password = "admin";
    //填写AMQ的IP
    private static String url = "tcp://10.10.3.186:61616";
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        //创建AMQ的连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(connectionFactory);
        //使用连接池连接
        activeMQComponent.setUsePooledConnection(true);
        //事务
        activeMQComponent.setTransacted(true);
        //客户端确认
        activeMQComponent.setAcknowledgementMode(3);
        //持久化
        activeMQComponent.setDeliveryPersistent(true);
        context.addComponent("activemq", activeMQComponent);
        System.out.println(url + " " + user + " " + password);
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute2());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
    private static int count = 1;
    public static RouteDefinition initRoute2(){
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition
                .from("jetty:http://localhost:8080/camel")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("访问成功!");
                        String method = exchange.getIn().getHeader(Exchange.HTTP_METHOD).toString();
                        System.out.println("method = " + method);
                        if(method.equals("GET")){
                            //这里要知道是否有参数，否则无参数的时候 Exchange.HTTP_QUERY 是得不到的
                            Object getBody = exchange.getIn().getHeader(Exchange.HTTP_QUERY);
                            if(getBody != null){
                                System.out.println("get方式传递的参数getBody = " + getBody.toString());
                            }
                        }else{
                            String body = exchange.getIn().getBody(String.class);
                            System.out.println("post方式传递的参数body = " + body);
                        }
                        String content="<?xml version=\"1.0\" encoding=\"utf-8\"?>  <soapenv:Envelope"
                                + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                                + "<soapenv:Body> " +
                                "        <m:sayHell xmlns:m=\"http://service.wenx.com/\" > " +
                                "            <arg0>112</arg0>  " +
                                "            <arg1>wen</arg1>  " +
                                "        </m:sayHell> " +
                                "    </soapenv:Body> "
                                + "</soapenv:Envelope>";
                        exchange.getOut().setBody(content);
                    }
                })
                .to("cxf:"
                                + "http://localhost:8082/wx_service/WebServiceImpl" //service address
                                + "?"
                                + "wsdlURL=http://localhost:8082/wx_service/WebServiceImpl?wsdl"    //wsdl url
 /*                       + "&"
                        + "serviceClass=service.wenx.com/WebServiceImpl"*/
                                + "&"
                                + "dataFormat=RAW"        //dataformat type
                )
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        //解析exchange中的返回值
                        String body = exchange.getIn().getBody(String.class);
                        String content="<?xml version=\"1.0\" encoding=\"utf-8\"?>  <soapenv:Envelope"
                                + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                                + "<soapenv:Body> " +
                                "        <m:getHell xmlns:m=\"http://service.wenx.com/\" > " +
                                "            <arg0>hello world!</arg0>  " +
                                "        </m:getHell> " +
                                "    </soapenv:Body> "
                                + "</soapenv:Envelope>";
                        exchange.getOut().setBody(content);
                    }
                })
                .to("cxf:"
                        + "http://localhost:8082/wx_service/WebServiceImpl" //service address
                        + "?"
                        + "wsdlURL=http://localhost:8082/wx_service/WebServiceImpl?wsdl"    //wsdl url
                        + "&"
                        + "dataFormat=RAW"        //dataformat type
                );
        return routeDefinition;
    }
}

